package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.excel.StudentModel;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.service.impl
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> pageQuery(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        Page<Student> pageInfo =studentMapper.pageQuery(studentQueryParam);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getResult());
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(student.getCreateTime());
        studentMapper.insert(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void updateById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    @Override
    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.updateViolation(id,score);
    }

    @Override
    public void importStuddent(List<StudentModel> studentModelList, Integer clazzId) {
        // 学历信息
        Map<String,Integer> degreeMap = new HashMap<>();
        degreeMap.put("初中",1);
        degreeMap.put("高中",2);
        degreeMap.put("大专",3);
        degreeMap.put("本科",4);
        degreeMap.put("硕士",5);
        degreeMap.put("博士",6);
        degreeMap.put("其他",7);
        List<Student> stuList = studentModelList.stream().map(sm -> {
            //把StudentModel转成Student对象
            Student student = new Student();
            student.setName(sm.getName());
            student.setNo(sm.getNo());
            student.setGender("男".equals(sm.getGenderStr()) ? 1 : 0);
            student.setPhone(sm.getPhone());
            student.setIdCard(sm.getIdCard());
            student.setIsCollege("是".equals(sm.getIsCollegeStr()) ? 1 : 0);
            student.setAddress(sm.getAddress());
            student.setDegree(degreeMap.get(sm.getDegreeStr()));
            student.setGraduationDate(sm.getGraduationDate());
            student.setClazzId(clazzId);
            student.setCreateTime(LocalDateTime.now());
            student.setUpdateTime(student.getCreateTime());
            return student;
        }).toList();

        studentMapper.insertBatch(stuList);
    }
}
